package com.itboye.textileexpes.presenter;

import java.util.Observer;

import android.util.Log;

import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.core.interfaces.IUserRepository;
import com.itboye.textileexpes.beans.PersonDataBean;
import com.itboye.textileexpes.beans.UserBean;
import com.itboye.textileexpes.interfaces.IUser;
import com.itboye.textileexpes.repository.UserReposit;
import com.itboye.textileexpes.repository.UserRepository;
import com.itboye.textileexpres.base.BasePresenter;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.DebugLog;

public class MainPresenter extends BasePresenter {

	// 登录
	public static final String LoginSuccessEvent = MainPresenter.class
			.getName() + "_Login_success";
	public static final String LoginFailureEvent = MainPresenter.class
			.getName() + "_Login_failure";

	// 登录
	public static final String PayNowSuccess = MainPresenter.class.getName()
			+ "_PayNowSuccess";
	public static final String PayNowFail = MainPresenter.class.getName()
			+ "_PayNowFail";
	// 自动登录
	public static final String AutoLoginSuccess = MainPresenter.class.getName()
			+ "_AutoLoginSuccess";
	public static final String AutoLoginFail = MainPresenter.class.getName()
			+ "_AutoLoginFail";

	// 注册
	public static final String RegisterSuccessEvent = MainPresenter.class
			.getName() + "_Register_success";
	public static final String RegisterFailureEvent = MainPresenter.class
			.getName() + "_Register_failure";

	// 修改密码
	public static final String UpdatePwdByPhoneFailureEvent = MainPresenter.class
			.getName() + "_UPdatePwd_failure";
	public static final String UpdatePwdByPhoneSuccessEvent = MainPresenter.class
			.getName() + "_UpdatePwd_success";

	// 更新个人资料
	public static final String UpdateUserInfoSuccessEvent = MainPresenter.class
			.getName() + "_UpdateUsrInfo_success";
	public static final String UpdateUserInfoFailureEvent = MainPresenter.class
			.getName() + "_UpdateUsrInfo_fail";

	// 绑定手机号
	public static final String BindPhoneNumberFailureEvent = MainPresenter.class
			.getName() + "_BindPhoneNumber_fail";
	public static final String BindPhoneNumberSuccessEvent = MainPresenter.class
			.getName() + "_BindPhoneNumber_success";

	// 通過密碼修改密碼
	public static final String UpdatePswByPwdFailureEvent = MainPresenter.class
			.getName() + "_Updatepsw_fail";
	public static final String UpdatePswByPwdSuccessEvent = MainPresenter.class
			.getName() + "_Updatepsw_success";

	// 修改绑定手机号
	public static final String UpdateBindPhoneFailureEvent = MainPresenter.class
			.getName() + "_UpdateBindPhone_fail";
	public static final String UpdateBindPhoneSuccessEvent = MainPresenter.class
			.getName() + "_UpdateBindPhone_success";

	public static final String WxPaySuccess = MainPresenter.class.getName()
			+ "_WxPay_success";
	public static final String WxPayFail = MainPresenter.class.getName()
			+ "_WxPay_fail";
	// 版本号
	public static final String getVersionSuccess = MainPresenter.class
			.getName() + "_getVersion_success";
	public static final String getVersionFail = MainPresenter.class.getName()
			+ "_getVersion_fail";

	// 验证码登录
	public static final String LongibyCodeSuccess = MainPresenter.class
			.getName() + "_LongibyCode_success";
	public static final String LongibyCodeFail = MainPresenter.class.getName()
			+ "_LongibyCode_fail";

	// 不敢兴起
	public static final String ProduceSuccess = MainPresenter.class.getName()
			+ "_Produce_success";
	public static final String ProduceFail = MainPresenter.class.getName()
			+ "_Produce_fail";

	public MainPresenter(Observer observer) {
		super(observer);
	}

	public void payNow(String uid, String orderCode, String payType,
			String payMoney) {
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "login success");
						result.setEventTag(Tag_Success);
						result.setEventType(PayNowSuccess);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "login failure");
						result.setEventTag(Tag_Error);
						result.setEventType(PayNowFail);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.payNow(uid, orderCode, payType, payMoney);
	}

	public void login(String phoneNumber, String pwd, String countryNo,
			String device_token) {
		IUser<UserBean> repo = new UserReposit(new ICompleteListener() {

			@Override
			public void success(ResultEntity result) {
				Log.v("MainPresenter", "login success");
				result.setEventTag(Tag_Success);
				result.setEventType(LoginSuccessEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}

			@Override
			public void failure(ResultEntity result) {
				Log.v("MainPresenter", "login failure");
				result.setEventTag(Tag_Error);
				result.setEventType(LoginFailureEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}
		});

		UserBean model = new UserBean();
		model.setUsername(phoneNumber);
		model.setPassword(pwd);
		model.setCountryNo(countryNo);
		model.setDevice_token(device_token);
		model.setDevice_type("android");
		// model.setType(1);
		repo.login(model);
	}

	public void register(String code, String country, String phonNumber,
			String password, String from) {

		DebugLog.v("MainPresenter", "register start");

		IUser<UserBean> repo = new UserReposit(new ICompleteListener() {

			@Override
			public void success(ResultEntity result) {
				Log.v("MainPresenter", "register success");
				result.setEventTag(Tag_Success);
				result.setEventType(RegisterSuccessEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}

			@Override
			public void failure(ResultEntity result) {
				Log.v("MainPresenter", "register failure");
				result.setEventTag(Tag_Error);
				result.setEventType(RegisterFailureEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}
		});

		UserBean model = new UserBean();
		model.setCode(code);
		model.setCountryNo(country);
		model.setUsername(phonNumber);
		model.setPassword(password);
		model.setRegFrom(from);
		repo.register(model);
	}

	public void updatePasswordByTelephone(String code, String country,
			String pwd, String phoneNumber) {
		IUser<UserBean> repo = new UserReposit(new ICompleteListener() {

			@Override
			public void success(ResultEntity result) {
				Log.v("MainPresenter", "register success");
				result.setEventTag(Tag_Success);
				result.setEventType(UpdatePwdByPhoneSuccessEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}

			@Override
			public void failure(ResultEntity result) {
				Log.v("MainPresenter", "register failure");
				result.setEventTag(Tag_Error);
				result.setEventType(UpdatePwdByPhoneFailureEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}
		});

		UserBean model = new UserBean();
		model.setUsername(phoneNumber);
		model.setPassword(pwd);
		model.setCountryNo(country);
		model.setCode(code);
		model.setType(1);
		repo.update(model);
	}

	public void updatePasswordByPwd(String uid, String newPwd, String pwd) {
		IUser<UserBean> repo = new UserReposit(new ICompleteListener() {

			@Override
			public void success(ResultEntity result) {
				Log.v("MainPresenter", "register success");
				result.setEventTag(Tag_Success);
				result.setEventType(UpdatePswByPwdSuccessEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}

			@Override
			public void failure(ResultEntity result) {
				Log.v("MainPresenter", "register failure");
				result.setEventTag(Tag_Error);
				result.setEventType(UpdatePswByPwdFailureEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}
		});

		UserBean model = new UserBean();
		model.setUid(uid);
		model.setType(5);
		model.setPassword(pwd);
		model.setNewpassword(newPwd);
		repo.update(model);
	}

	public void updateUserInfoByUid(UserBean model) {
		/**
		 * uid是用户IDmobile否用户手机realname否真实姓名email否邮箱birthday否生nickname否昵称sex否性别(0
		 * :女,1:男 , 其它值:未知)qq否qq号head
		 */
		IUser<UserBean> repo = new UserReposit(new ICompleteListener() {

			@Override
			public void success(ResultEntity result) {
				Log.v("MainPresenter", "register success");
				result.setEventTag(Tag_Success);
				result.setEventType(UpdateUserInfoSuccessEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}

			@Override
			public void failure(ResultEntity result) {
				Log.v("MainPresenter", "register failure");
				result.setEventTag(Tag_Error);
				result.setEventType(UpdateUserInfoFailureEvent);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}
		});
		model.setType(2);
		repo.update(model);
	}

	public void bindPhoneNumber(PersonDataBean model) {
		/**
		 * uid是用户IDmobile否用户手机realname否真实姓名email否邮箱birthday否生nickname否昵称sex否性别(0
		 * :女,1:男 , 其它值:未知)qq否qq号head
		 */
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(BindPhoneNumberFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(BindPhoneNumberSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.update(model);
	}

	public void updateBindPhoneNumber(PersonDataBean model) {
		/**
		 * 更换绑定手机号码
		 * */
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(UpdateBindPhoneSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(UpdateBindPhoneFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.update(model);
	}

	public void wxPay(String orderCode, String totalPrice, String desc) {
		// TODO Auto-generated method stub
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(WxPaySuccess);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(WxPayFail);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.wxPay(orderCode, totalPrice, desc);
	}

	public void testPay(String uid, String code) {
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(getVersionSuccess);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(getVersionFail);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.testPay(uid, code);
	}

	public void onLoginByCode(String country, String mobile, String code) {
		IUser<UserBean> repo = new UserReposit(new ICompleteListener() {

			@Override
			public void success(ResultEntity result) {
				Log.v("MainPresenter", "register success");
				result.setEventTag(Tag_Success);
				result.setEventType(LongibyCodeSuccess);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}

			@Override
			public void failure(ResultEntity result) {
				Log.v("MainPresenter", "register failure");
				result.setEventTag(Tag_Error);
				result.setEventType(LongibyCodeFail);
				MainPresenter.this.setChanged();
				MainPresenter.this.notifyObservers(result);
			}
		});

		repo.onLoginByCode(country, mobile, code);
	}

	public void Autologin(UserBean model) {
		// TODO Auto-generated method stub
		IUser<UserBean> repo = new UserReposit(new ICompleteListener() {

			@Override
			public void success(ResultEntity result) {
				Log.v("MainPresenter", "register success");
				result.setEventTag(Tag_Success);
				result.setEventType(AutoLoginSuccess);
				setChanged();
				notifyObservers(result);
			}

			@Override
			public void failure(ResultEntity result) {
				Log.v("MainPresenter", "register failure");
				result.setEventTag(Tag_Error);
				result.setEventType(AutoLoginFail);
				setChanged();
				notifyObservers(result);
			}
		});
		repo.Autologin(model);
	}

}
